package obsluga;

import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import org.jfree.chart.JFreeChart;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.DefaultFontMapper;
import com.lowagie.text.pdf.FontMapper;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

class JavaFilter3 extends javax.swing.filechooser.FileFilter {
	@Override
	public boolean accept(File f) {
		return f.getName().toLowerCase().endsWith(".pdf") || f.isDirectory();
	}

	@Override
	public String getDescription() {
		return "files (*.pdf)";
	}
}

public class zapisDoPDFA {

	JavaFilter3 fJavaFilter3 = new JavaFilter3();
	File fFile = new File("domyslny.txt");

	public boolean zapiszPlik(int wybor, JFreeChart chart) {
		JFileChooser fc = new JFileChooser();

		fc.setCurrentDirectory(new File("."));

		// dodanie domyślnej nazwy do pliku do zapisania
		fc.setSelectedFile(new File("wykres.pdf"));
		fc.setFileFilter(fJavaFilter3);

		// otwieranie okna
		int result = fc.showSaveDialog(null);
		if (result == JFileChooser.CANCEL_OPTION) {
			return true;
		} else if (result == JFileChooser.APPROVE_OPTION) {
			fFile = fc.getSelectedFile();
			if (fFile.exists()) {
				int potwierdzenie = JOptionPane.showConfirmDialog(null,
						"Czy napewno nadpisać plik?",
						"Potwierdzenie nadpisania",
						JOptionPane.OK_CANCEL_OPTION,
						JOptionPane.QUESTION_MESSAGE);
				if (potwierdzenie == JOptionPane.CANCEL_OPTION)
					return false;
			}
			try {
				saveChartAsPDF(fFile, chart, 400, 300, new DefaultFontMapper());

				JOptionPane.showMessageDialog(
						null,
						"Wykres został zapisany w miejscu: "
								+ fFile.getCanonicalPath());
			} catch (IOException exc) {
			}

			return true;
		} else {
			return false;
		}

	}

	/*
	 * Metody potrzebne do zapisywania do pliku pdf
	 * 
	 * @zapisywanie do pliku PDF
	 */
	public static void saveChartAsPDF(File file, JFreeChart chart, int width,
			int height, FontMapper mapper) throws IOException {
		OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
		writeChartAsPDF(out, chart, width, height, mapper);
		out.close();

	}

	public static void writeChartAsPDF(OutputStream out, JFreeChart chart,
			int width, int height, FontMapper mapper) throws IOException {
		Rectangle pagesize = new Rectangle(width, height);
		Document document = new Document(pagesize, 50, 50, 50, 50);
		try {
			PdfWriter writer = PdfWriter.getInstance(document, out);
			document.addAuthor("JFreeChart");
			document.addSubject("Zapis do pdf");
			document.open();
			PdfContentByte cb = writer.getDirectContent();
			PdfTemplate tp = cb.createTemplate(width, height);
			Graphics2D g2 = tp.createGraphics(width, height, mapper);
			Rectangle2D r2D = new Rectangle2D.Double(0, 0, width, height);
			chart.draw(g2, r2D);
			g2.dispose();
			cb.addTemplate(tp, 0, 0);
		} catch (DocumentException de) {
		}
		document.close();
	}

	/*
	 * koniec metod potrzebnych do zapisywania do pdf
	 */

}
